package com.ruoyi.wms.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsMaterialuse;
import com.ruoyi.wms.service.IWmsMaterialuseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料使用量Controller
 * 
 * @author chao
 * @date 2024-07-28
 */
@Controller
@RequestMapping("/wms/materialuse")
public class WmsMaterialuseController extends BaseController
{
    private String prefix = "wms/materialuse";

    @Autowired
    private IWmsMaterialuseService wmsMaterialuseService;

    @Autowired
    private IMainUnitesService mainUnitesService;

    @RequiresPermissions("wms:materialuse:view")
    @GetMapping()
    public String materialuse()
    {
        return prefix + "/materialuse";
    }

    /**
     * 查询物料使用量列表
     */
    @RequiresPermissions("wms:materialuse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsMaterialuse wmsMaterialuse)
    {
        startPage();
        List<WmsMaterialuse> list = wmsMaterialuseService.selectWmsMaterialuseList(wmsMaterialuse);
        return getDataTable(list);
    }

    /**
     * 导出物料使用量列表
     */
    @RequiresPermissions("wms:materialuse:export")
    @Log(title = "物料使用量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsMaterialuse wmsMaterialuse)
    {
        List<WmsMaterialuse> list = wmsMaterialuseService.selectWmsMaterialuseList(wmsMaterialuse);
        ExcelUtil<WmsMaterialuse> util = new ExcelUtil<WmsMaterialuse>(WmsMaterialuse.class);
        return util.exportExcel(list, "物料使用量数据");
    }

    /**
     * 新增物料使用量
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/add";
    }

    /**
     * 新增保存物料使用量
     */
    @RequiresPermissions("wms:materialuse:add")
    @Log(title = "物料使用量", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsMaterialuse wmsMaterialuse)
    {
        return toAjax(wmsMaterialuseService.insertWmsMaterialuse(wmsMaterialuse));
    }

    /**
     * 修改物料使用量
     */
    @RequiresPermissions("wms:materialuse:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsMaterialuse wmsMaterialuse = wmsMaterialuseService.selectWmsMaterialuseById(id);
        mmap.put("wmsMaterialuse", wmsMaterialuse);
        mmap.put("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/edit";
    }

    /**
     * 修改保存物料使用量
     */
    @RequiresPermissions("wms:materialuse:edit")
    @Log(title = "物料使用量", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsMaterialuse wmsMaterialuse,Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return toAjax(wmsMaterialuseService.updateWmsMaterialuse(wmsMaterialuse));
    }

    /**
     * 删除物料使用量
     */
    @RequiresPermissions("wms:materialuse:remove")
    @Log(title = "物料使用量", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsMaterialuseService.deleteWmsMaterialuseByIds(ids));
    }


    @RequiresPermissions("process:order:remove")
    @Log(title = "执行出库", businessType = BusinessType.OTHER)
    @PostMapping( "/excuteOutStorge")
    @ResponseBody
    public AjaxResult excuteOutStorge(@RequestParam("rowId") Long rowId, @RequestParam("materialId") Long materialId, @RequestParam("num") Double num)
    {
        return toAjax(wmsMaterialuseService.excuteOutStorge(rowId,materialId,num));
    }

}
